#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入相应的库文件
import requests
import re
import mysql.connector
import sys
dict = [('0', '其他小说'), ('1', '玄幻奇幻'), ('2', '修真仙侠'), ('3', '都市青春'), ('4', '军事历史'),
        ('5', '网游竞技'), ('6', '科幻灵异'), ('7', '言情穿越'), ('8', '耽美同人'), ('9', '台言古言'),
        ('10', '武侠小说'), ('11', '古典文学'), ('12', '侦探推理'), ('13', '外国文学'), ('14', '当代现代'),
        ('15', '童话故事'), ('16', '传记纪实'), ('17', '韩流青春'), ('18', '影视文学')]

mydb = mysql.connector.connect(
    host="192.168.1.4",  # 数据库主机地址
    port='3316',
    user="root",  # 数据库用户名
    database="test",  # 数据库名称 没有的话插入操作报错
    passwd="211314wccA"  # 数据库密码

)
mycursor = mydb.cursor()

def geturl_into_excel(i):
    row = 1
    for j in range(99999):
        if j < 132:
            continue
        html = requests.get("https://ixdzs8.com/sort/" + i.__str__() + "/index-0-0-0-" + (j + 1).__str__() + ".html")
        html.encoding = 'utf-8'
        # print(html.text)
        labelfind = '<h3 class="bname"><a href="/read/(.*?)/"  title="(.*?)">'
        labels = re.findall(labelfind, html.text, re.S)
        print(
            "===========" + dict[i][1].__str__() + "--> 第" + (j + 1).__str__() + "页================================")
        print(labels)
        if len(labels) == 0:
            print("数组为空")
            break
        for (key, value) in labels:
            book_type_insert_sql = "INSERT INTO fiction_table_new (f_code, f_name,type_id,type_name) VALUES ('" + key.__str__() + "', '" + value.__str__() + "', '" + i.__str__() + "', '" + \
                                   dict[i][1].__str__() + "') "
            mycursor.execute(book_type_insert_sql)
            mydb.commit()
            print("==================第" + (
                row).__str__() + "行" + value.__str__() + "================================")
            row += 1

def get_url_and_author(url):
    html = requests.get(url)
    html.encoding = 'utf-8'
    print("==================================================")
    print(html.text)
    print("==================================================")
    labelfind = '<title>(.*?)</title>'
    labels = re.findall(labelfind, html.text, re.S)
    print(labels[0])
    labelfindurl = '<span class="btn-solid"><a href="(.*?)">TXT下载</a></span>'
    labelurls = re.findall(labelfindurl, html.text, re.S)
    print(labelurls[0])

def test(url):
    html = requests.get(url)
    html.encoding = 'utf-8'
    print("==================================================")
    print(html.text)
    print("==================================================")
    labelfind = '<h3 class="bname"><a href="/read/(.*?)/"  title="(.*?)">'
    labels = re.findall(labelfind, html.text, re.S)
    print("==================================================")
    print(labels)
    labelfind = '<title>(.*?)</title>'
    labels = re.findall(labelfind, html.text, re.S)
    print("==================================================")
    print(labels)




# 程序主入口
if __name__ == '__main__':
    if len(sys.argv) > 1:
        print("传递的参数：", sys.argv[1])
        geturl_into_excel(sys.argv[1])
    else:
        print("没有传递参数")

    #geturl_into_excel(1)

